<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-05-08
 * Time: 15:56:19
 * Info:
 */

namespace App\admin\controller\system;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\admin\controller\CommonController;
use app\common\model\LoginLog as LoginLogModel;
use app\common\model\SystemLog as SystemLogModel;
use support\Request;

/**
 * @ControllerAnnotation(title="日志管理")
 * Class Node
 * @package app\admin\controller\system
 */
class LogController extends CommonController
{

    /**
     * @NodeAnotation(title="登录日志")
     */
    public function loginLog(Request $request)
    {
        if ($request->isAjax()) {
            $limit = (int)$request->input('limit', 10);
            $page  = (int)$request->input('page', 1);
            $first = ($page - 1) * $limit;
            $count = LoginLogModel::count();
            $list  = LoginLogModel::limit($first, $limit)->order('id desc')->select();
            foreach ($list as $key => $v) {
                $area = ! empty($v['area']) ? "-".$v['area'] : "";
                $city = ! empty($v['city']) ? "-".$v['city'] : "";

                $list[$key]['address'] = $v['country'].$area.$city;
                $list[$key]['status']  = strstr($v['desc'], 'success') ? 1 : 0;
            }

            return $this->success('ok', $list, $count);
        }

        return view('log/login_log');
    }

    /**
     * @NodeAnotation(title="操作日志")
     */
    public function systemLog(Request $request)
    {
        if ($request->isAjax()) {
            $limit = (int)$request->input('limit', 10);
            $page  = (int)$request->input('page', 1);
            $first = ($page - 1) * $limit;
            $count = SystemLogModel::count();
            $list  = SystemLogModel::with([
                'admin' => function ($query) {
                    $query->Field('id,username,nickname');
                }
            ])->limit($first, $limit)->order('id desc')->select();
            foreach ($list as $key => $v) {
                $list[$key]['adminname'] = $v['admin']['username'] ?? '';
                $list[$key]['nickname']  = $v['admin']['nickname'] ?? '';
            }

            return $this->success('ok', $list, $count);
        }

        return view('log/system_log');
    }

}
